
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class LoginPrompt extends Frame {
	static boolean useAuth = false;
	JTextField username;
	JPasswordField password;
	JPanel panel;
	static String appPassword = "";
	static boolean savePassword = false;
	String filepath = System.getProperty("user.home")+"\\EasyFTP";
	String loginFolder = System.getProperty("user.home")+"\\EasyFTP\\Saved";
	//String user = System.getProperty("user.home")+"\\EasyFTP\\Saved\\"+username.getText();
	JFrame frame = null;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	public void makeFrame(){
		frame = new JFrame();
		panel = new JPanel(new GridLayout(5,2));
		JLabel gmail = new JLabel("Gmail credentials:");
		JLabel gmail1 = new JLabel("Please log in");
		JLabel u = new JLabel("Username: ");
		JLabel p = new JLabel("Password: ");
		username = new JTextField();
		password = new JPasswordField(20);
		JButton submit=new JButton("Submit");
		submit.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
				if(loginAttempt(username.getText(), password.getPassword()) == false)
					JOptionPane.showMessageDialog(frame, "Incorrect username or password, please try again");
			}
			
		});
		username.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					frame.dispose();
					if(loginAttempt(username.getText(), password.getPassword()) == false)
						JOptionPane.showMessageDialog(frame, "Incorrect username or password, please try again");
				}
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				
			}
			
		});
		password.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					frame.dispose();
					if(loginAttempt(username.getText(), password.getPassword()) == false){
							JOptionPane.showMessageDialog(frame, "Incorrect username or password, please try again");
						}
				}
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				
			}
			
		});
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
			}
			
		});
		panel.add(gmail);
		panel.add(gmail1);
		panel.add(u);
		panel.add(username);
		if(useAuth == false)
			panel.add(p);
		if(useAuth == false)
			panel.add(password);
		panel.add(submit);
		panel.add(cancel);
		frame.add(panel);
		panel.setVisible(true);
		frame.setTitle("GMail Login");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
		frame.setSize(350,250);
		//Set to the middle of the screen
		frame.setLocationRelativeTo(null);
		panel.updateUI();
		username.requestFocus();
	}
	
	public boolean loginAttempt(String u, char[] p){
		 try{
			 Properties props = new Properties();
			 	props.put("mail.debug", "true");
				props.setProperty("mail.imap.host", "imap.gmail.com");
				props.setProperty("mail.imap.port", "993");
				props.setProperty("mail.imap.connectiontimeout", "5000");
				props.setProperty("mail.imap.timeout", "5000");
				props.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
				props.setProperty("mail.imap.socketFactory.fallback", "false");
				props.setProperty("mail.smtp.host", "smtp.gmail.com");
				props.setProperty("mail.smtp.port", "465");
				Session session = Session.getDefaultInstance(props,null);
				Store store = session.getStore("imaps");
				if(appPassword == ""){
					String pass = String.valueOf(p);
					store.connect("imap.gmail.com",u,pass);
					store.close();
				}
				else{
					store.connect("imap.gmail.com", u, appPassword);
					store.close();
				}
				
				SendEmail mail = new SendEmail(u, String.valueOf(p));
				mail.makeFrame();
				return true;
		 }
		 catch(AuthenticationFailedException e){
			 username.setText("");
			 password.setText("");
		 	 username.requestFocusInWindow();
			 return false;
		 }
		 catch(MessagingException e){
			 password.setText("");
			 username.setText("");
			 username.requestFocusInWindow();
			 return false;
		 }
		 
	}
	
	public void updatePanel(){
		panel.updateUI();
	}
}
